<template>
    <div>
        <el-row>
            <el-col :span="24" class="base-mian-cell" v-if="appConsignorFlag">
                <div class="base-mian-cell-title clearfix">
                    <div class="left fl">
                        <span class="line">|</span>
                        <span class="tips">委托人信息</span>
                    </div>
                    <div class="right fr" @click="changeView">
                        <i class="el-icon-circle-close"></i>
                    </div>
                </div>
                <div class="base-mian-cell-main editInput">
                    <el-row class="mb10">
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">姓名</label>
                            </el-col>
                            <el-col :span="14">
                                <el-input :disabled="allInfoReadOnly" v-model="trustClient.surname"></el-input>
                            </el-col>
                        </el-col>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">国家地区</label>
                            </el-col>
                            <el-col :span="14">
                                <el-select placeholder="请选择" v-if="allInfoReadOnly" disabled
                                           v-model="trustClient.ctrycode">
                                    <el-option
                                            v-for="item in nations"
                                            :key="item.value"
                                            :label="item.option"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                <el-select placeholder="请选择" v-else v-model="trustClient.ctrycode">
                                    <el-option
                                            v-for="item in nations"
                                            :key="item.value"
                                            :label="item.option"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-col>
                        </el-col>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">证件类型</label>
                            </el-col>
                            <el-col :span="14">
                                <el-select placeholder="请选择" v-if="allInfoReadOnly" disabled
                                           v-model="trustClient.cflag">
                                    <el-option
                                            v-for="item in cflags"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                <el-select placeholder="请选择" v-else v-model="trustClient.cflag">
                                    <el-option
                                            v-for="item in cflags"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-col>
                        </el-col>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">证件号码</label>
                            </el-col>
                            <el-col :span="14">
                                <el-input :disabled="allInfoReadOnly" v-model="trustClient.secuityno"></el-input>
                            </el-col>
                        </el-col>
                    </el-row>
                    <el-row class="mb10">
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">移动电话</label>
                            </el-col>
                            <el-col :span="14">
                                <el-input :disabled="allInfoReadOnly" v-model="trustClient.rmblphone"></el-input>
                            </el-col>
                        </el-col>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">固定电话</label>
                            </el-col>
                            <el-col :span="14">
                                <el-input :disabled="allInfoReadOnly" v-model="trustClient.cltphone"></el-input>
                            </el-col>
                        </el-col>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">性别</label>
                            </el-col>
                            <el-col :span="14">
                                <el-select placeholder="请选择" v-if="allInfoReadOnly" disabled
                                           v-model="trustClient.cltsex">
                                    <el-option
                                            v-for="item in cltsex"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                <el-select placeholder="请选择" v-else v-model="trustClient.cltsex">
                                    <el-option
                                            v-for="item in cltsex"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-col>
                        </el-col>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">出生日期</label>
                            </el-col>
                            <el-col :span="14">
                                <!--                                <el-date-picker-->
                                <!--                                        type="date"-->
                                <!--                                        placeholder="选择日期" v-model="trustClient.cltdob">-->
                                <!--                                </el-date-picker>-->
                                <el-input :disabled="allInfoReadOnly" v-model="trustClient.cltdob"></el-input>
                            </el-col>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">与被保险人关系</label>
                            </el-col>
                            <el-col :span="14">
                                <el-select placeholder="请选择" v-if="allInfoReadOnly" disabled
                                           v-model="trustClient.fmlyreln">
                                    <el-option
                                            v-for="item in fmlyrelns"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                <el-select placeholder="请选择" v-else v-model="trustClient.fmlyreln">
                                    <el-option
                                            v-for="item in fmlyrelns"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-col>
                        </el-col>
                        <el-col :span="6">
                            <el-col :span="10">
                                <label class="complain-lab">年收入（万元）</label>
                            </el-col>
                            <el-col :span="14">
                                <el-input :disabled="allInfoReadOnly" v-model="trustClient.owneranne"></el-input>
                            </el-col>
                        </el-col>
                        <el-col :span="12">
                            <el-col :span="5">
                                <label class="complain-lab">地址</label>
                            </el-col>
                            <el-col :span="19">
                                <el-input :disabled="allInfoReadOnly" v-model="trustClient.adddesc"></el-input>
                            </el-col>
                        </el-col>
                    </el-row>
                    <div class="edit-main" v-if="!allInfoReadOnly">
                        <button @click="saveInfo">保存</button>
                    </div>
                </div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
  import {
    querySelectNation,
    findTrustClinetByApplnum,
    saveTrustClintByApplnum,
    checkWDfor400
  } from "@/api/underwriting/uw";
  import uwconstant from '@/utils/uwConstants';
  import constant from '@/utils/constant';

  export default {
    data() {
      return {
        options: [
          {
            label: '测试',
            value: '01'
          }
        ],
        trustClient: {
          surname: "",//姓名
          ctrycode: "",//国籍
          cflag: "",//证件类型
          secuityno: "", //证件号码
          rmblphone: "", //移动电话
          cltphone: "", //固定电话
          adddesc: "", // 地址
          cltsex: "", //性别 M/F
          cltdob: "", // 出生日期
          fmlyreln: "", // 与被保险人关系
          owneranne: "", // 年收
        },
        cltsex: uwconstant.cltsex,
        fmlyrelns: uwconstant.fmlyrelns,
        cflags: uwconstant.cflags,
        nations: [],
        euaaData:{},
        subUnLoading:false
      }
    },
    props: {
      appConsignorFlag: {
        type: Boolean,
        default: false
      },
      allInfoReadOnly: {
        type: Boolean,
        default: false
      },
      applnum: {
        type: String,
        default: ""
      },
      euaa: {
        type: String,
        default: ""
      }
    },
    methods: {
      changeView() {
        this.$emit('subConsignorFlag', false);
      },

      popMsg(msg, type) {
        this.$message({
          message: msg,
          type: type
        });
      },
      async check400(){
        let procflg = "";
          let params = {
            "applnum":this.applnum
          }
          await checkWDfor400(params).then(res => {
            if (res.resultCode == constant.SUCCESS) {
              procflg = res.data;
            }
          }).catch(err => {

          })
        return procflg;
      },

      async saveInfo() {
        let that= this;
        let procflg = await this.check400();
        if(procflg){
          if(procflg=="4" || procflg=="5" || procflg=="6"){
            that.popMsg("保单400状态为"+procflg+",请核实",'error');
            return false;
          }
        }

          let tcn_surname = this.trustClient.surname;
          if (tcn_surname == null || tcn_surname == "" || tcn_surname.length == 0) {
            this.popMsg("注意:委托人姓名为空!", "error");
            this.trustClient.surname = "";
          }
          let ftcn_cltdob = this.trustClient.cltdob; // 出生日期

          //验证出生日期
          let tcn_cltdob = this.uncastDate(ftcn_cltdob);
          if(tcn_cltdob=="error"){
            this.popMsg("日期格式错误, 正确格式为xxxx/xx/xx!", "error");
            return false;
          }

          // this.$emit('showLoading');
          this.trustClient['applnum'] = this.applnum;
          this.trustClient['chdrnum'] = this.euaaData.as_pol1+this.euaaData.as_pol2+this.euaaData.as_pol3;
          let trcl = JSON.parse(JSON.stringify(this.trustClient));
          trcl.cltdob = tcn_cltdob;
          let params = {
            "trcl":JSON.stringify(trcl)
          }
          that.$emit('showLoading',true);
          saveTrustClintByApplnum(params).then(res => {
            that.$emit('showLoading',false);
            if (res.resultCode == constant.SUCCESS) {
              this.popMsg("保存成功","success")
            }else{
              this.popMsg(res.resultMsg,"error")
            }
          }).catch(err => {
            console.log(err);
          })

      },
      initData() {
        let that = this;
        that.$emit('showLoading',true);
        querySelectNation().then(res => {
          that.$emit('showLoading',false);
          if (res.resultCode == constant.SUCCESS && res.data !=null) {
            this.nations = res.data;
          }
        }).catch(err => {
          console.log(err)
        })
        this.showClient();
        this.euaaData = JSON.parse(this.euaa);
      },
      //将值转换成日期形式
      uncastDate(val) {
        let newVal = null;
        if (val == "0000/00/00" || val == null || val == "" || val.length == 0) {
          newVal = "0";
        } else {
          var reg = /^\d{4}(\/)\d{2}\1\d{2}$/;
          if (reg.test(val)) {
            newVal = val.substring(0, 4) + val.substring(5, 7) + val.substring(8, 10);
          } else {
            newVal = "error";
          }
        }
        return newVal;
      },
      //委托人
      showClient() {
        let that= this;
        let params = {
          "applnum": this.applnum
        }
        that.$emit('showLoading',true);
        findTrustClinetByApplnum(params).then(res => {
          that.$emit('showLoading',false);
          if (res.resultCode == constant.SUCCESS && res.data != null) {
            this.trustClient = res.data;
            let cltdob = this.trustClient.cltdob + '';
            if(cltdob.length == 8){
              this.trustClient.cltdob = cltdob.substring(0, 4) +'/'+ cltdob.substring(4, 6) +'/'+ cltdob.substring(6, 8);
            }
            if(cltdob == '0'){
              this.trustClient.cltdob = '';
            }
            if(this.trustClient.owneranne != null && this.trustClient.owneranne != undefined){
              this.trustClient.owneranne = this.trustClient.owneranne.toFixed(2);
            }
          }
        }).catch(err => {
          console.log(err)
        })
      },

    },
    created() {
      this.initData();
    }
  }
</script>
<style lang="less" scoped>

</style>